package com.xbongbong.paas.application;

import com.alibaba.fastjson.serializer.DoubleSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.xbongbong.pro.config.ProBaseConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author kaka
 */
@ServletComponentScan(basePackages = {"com.xbongbong.paas.filter"})
@SpringBootApplication
@MapperScan(basePackages = {"com.xbongbong.*.domain"}, sqlSessionFactoryRef = "sqlSessionFactory")
@ComponentScan(basePackages={"com.xbongbong","com.alibaba.dingtalk.openapi"})
@EnableElasticsearchRepositories(basePackages = "com.xbongbong.paas.elasticsearch")
@EnableFeignClients(basePackages = {"com.xbongbong.paas.manager.feign", "com.xbongbong.pro.message.feign", "com.xbongbong.paas.feign",  "com.xbongbong.schedule.feign", "com.xbongbong.*.feign"})
public class ProConnectionApplication {

	private static final Logger LOG = LoggerFactory.getLogger(ProConnectionApplication.class);

    @Resource
    private ProBaseConfig proBaseConfig;

	@Resource
	private RestTemplateBuilder builder;

	/**
	 * 使用RestTemplateBuilder来实例化RestTemplate对象，spring默认已经注入了RestTemplateBuilder实例
	 * @return RestTemplate实例
	 */
	@Bean
	public RestTemplate restTemplate() {
		return builder.build();
	}

	public static void main(String[] args) {
		System.setProperty("es.set.netty.runtime.available.processors", "false");
		try {
			// fastJson double位数，设成最多的6位，避免科学计数法
			SerializeConfig config = SerializeConfig.getGlobalInstance();
			config.put(Double.class, new DoubleSerializer("#.######"));

			SpringApplication.run(ProConnectionApplication.class, args);
		} catch (Exception e) {
			LOG.error("ProScriptApplication启动报错:", e);
		}
	}

}
